/*
Copyright (c) 2019 Alibaba Group Holding Limited

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
#include "stdio.h"
#include "vtimer.h"
#include "datatype.h"
#include "pmu.h"
// #include "stdlib.h"
#include "ascon_hash.h"
#include "ascon_aead_en.h"
#include "ascon_aead_de.h"

#define DMA_BY_DEP  0xfff
#define DMA_REE_INC_START_PC 0xf0000000 //ree的程序在tee的iram中的地址
#define DMA_BY_DEST_BASE     0x20025000 //定义dest地址
#define DMA_BY_SRC_BASE      0x00008000//定义src地址
#define DMA_BLK_NUM          0X01      //DMA搬移block的数量

#define IOPMP_BASE 0x40200000
#define IOPMP_RULE_REG 0X00
#define IOPMP_ERROR_REG 0X04

#define CODE_LEN_MAX 8
#define HASH_NUM_VALID 240
#define MAX_DEAL_LEN (HASH_NUM_VALID - 32)
#define CODENUM_U32 (HASH_NUM_VALID>>2)
#define ADDR_DATA_BEGIN 0x90000000

#define AEAD_NUM_VALID 56
#define BLOCK_NUM (AEAD_NUM_VALID>>3)
#define AEAD_CODENUM_U32 (AEAD_NUM_VALID/4)



uint32_t PAD(uint32_t i) { return 0x80ull << (24 - 8 * i); }



void crypto_hash(Ascon_Hash_InitTypeDef* ascon_hash_initstruct,u32 msg_data[],u32 length)
{
     uint32_t i;
 //   static Ascon_Hash_InitTypeDef ascon_hash_initstruct;
        //  for ( i = 0; i < CODENUM_U32; i=i+1)
        //  {
        //  printf("msg_data:%x\n",msg_data[i]);
        //   }
        ascon_hash_initstruct->msg_block_num = length;
        ascon_hash_initstruct->msg_addr = 0x00000000;
        ascon_hash_initstruct->hash_ctrl_mode = 0x00000001;
        ascon_hash_initstruct->hash_h_addr = 0x00000000;
        ascon_hash_initstruct->msg_data = msg_data;

        ascon_hash_initstruct->hash_ctrl_en = ENABLE;
        Ascon_Hash_Init(ascon_hash_initstruct);
        Start_Ascon_Hash(ascon_hash_initstruct);
        Get_Ascon_Hash(ascon_hash_initstruct);
//        hash_data_o = ascon_hash_initstruct.hash_data;
        //printf("hash_data_o 0x%x\n",*ascon_hash_initstruct->hash_data);
}

u32 hash_loop_readbuf(u32 loop_size,u32 msg_data[],u32 d_len,u32 *addr,u32 start_n)
{
    u32 i ;
    uint32_t buffer_data;
    //static uint32_t msg_data[CODENUM_U32+1];
    //  printf("loop_size:0x%x\n",loop_size); 
    for ( i = 0; i < loop_size; i = i+2)
    {
            // printf("addr0:0x%x\n",*addr + ((i)<<2)); 
            buffer_data = REG32(*addr + ((i)<<2));
            // printf("buffer_data0:0x%x\n",buffer_data); 
            // msg_data[i+start_n] = ((buffer_data & 0x000000ff) << 24) | ((buffer_data & 0x0000ff00) << 8) | ((buffer_data & 0x00ff0000) >> 8) | ((buffer_data & 0xff000000) >> 24);
            msg_data[i+start_n] = buffer_data;

            // printf("addr1:0x%x\n",*addr + ((i+1)<<2)); 
            buffer_data = REG32(*addr + ((i+1)<<2));
            // printf("buffer_data1:0x%x\n",buffer_data);  
            msg_data[i+1+start_n] = buffer_data;

    }
     msg_data[CODENUM_U32] = 0x00000000;
     msg_data[CODENUM_U32+1] = 0x80000000;
        //  for ( i = 0; i < CODENUM_U32; i=i+1)
        //  {
        //  printf("msg_data:0x%x\n",msg_data[i]);
        //   }
        // printf("before addr:%d\n",*addr);
        // printf("loop_size:%d\n",loop_size);
        // printf("loop_size<<2:%d\n",loop_size << 2);
     *addr = *addr + (loop_size << 2); //一定加括号，+优先级更高
    //  printf("addr:0x%x\n",*addr);
     return d_len-(loop_size << 2);
    
    
}


//测试用例
void run_ascon_hash(u32 codelen,u32 *data_addr)
{

    static u32 *hash_data;
    static Ascon_Hash_InitTypeDef ascon_hash_initstruct;
    u32 msg_data[CODENUM_U32+1];
    static u32 buffer_data;
    u32 i;
    //u32 codelen;
    u32 len_buff;
    u32 addr_begin = data_addr;
    
    u32 num;
    // codelen = CODE_LEN_MAX;
     //printf("0x0000_0000: 0x%x\n",REG32(0x00000000));

    if( codelen >= 240)
       {
            printf("codelen begin : %d\n",codelen);
           codelen = hash_loop_readbuf(CODENUM_U32,msg_data,codelen,&addr_begin,0);
           crypto_hash(&ascon_hash_initstruct,msg_data,HASH_NUM_VALID+8);
           hash_data = ascon_hash_initstruct.hash_data;
           
            for ( i = 0; i < 8; i= i+1) 
            {
               msg_data[i] = *(hash_data+i);
               //printf("hash is : 0x%x\n",*(hash_data+i));
            }
                
    
            // printf("len1 %d \n", codelen);
       }
      while (codelen >= 240-32)
      {
             codelen = hash_loop_readbuf(CODENUM_U32-8,msg_data,codelen,&addr_begin,8);
              crypto_hash(&ascon_hash_initstruct,msg_data,HASH_NUM_VALID+8);
              hash_data = ascon_hash_initstruct.hash_data;
               for ( i = 0; i < 8; i= i+1) 
                   msg_data[i] = *(hash_data+i);
               //printf("len2 %d \n", codelen);
             
      }
      if( codelen >= 0 )
      {
          num = codelen>>3;
        //   printf("num %d \n", num);
          len_buff = codelen - (num<<3);
             num = (num <<1);
          if(addr_begin != data_addr )
          {
              hash_loop_readbuf((num + 2),msg_data,codelen,&addr_begin,8);  //一个+2 loop就是8字节
             if(len_buff > 4)
             {
              msg_data[num+8] &= ~(0xffffffff>>((len_buff-4)<<3));
              msg_data[num+8] ^= PAD(len_buff-4);                    
             }
             else
             {
              msg_data[num+9] &= ~(0xffffffff>>(len_buff<<3));
              msg_data[num+9] ^= PAD(len_buff);
              msg_data[num+8] &= 0x00000000;
             }
            //  printf("msg_data3: 0x%x\n",msg_data[num+8]);
            //  printf("msg_data3: 0x%x\n",msg_data[num+9]);

            // printf("len3: %d\n",codelen + 40 -len_buff);
            crypto_hash(&ascon_hash_initstruct,msg_data,codelen + 40 -len_buff);
            hash_data = ascon_hash_initstruct.hash_data;
          }
          else
          {
              hash_loop_readbuf((num + 2),msg_data,codelen,&addr_begin,0);
                //    printf("lenth: %d\n",len_buff);
             if(len_buff > 4)
             {
              msg_data[num] &= ~(0xffffffff>>((len_buff-4)<<3));
              msg_data[num] ^= PAD(len_buff-4);                    
             }
             else if(len_buff == 4)
             {
                //    printf("lenth: %d\n",len_buff);
              msg_data[num] = 0x80000000;
             }else{
              msg_data[num+1] &= ~(0xffffffff>>(len_buff<<3));
              msg_data[num+1] ^= PAD(len_buff);
              msg_data[num] &= 0x00000000;
             }

            // printf("msg_data%d: 0x%x\n",num+1,msg_data[num+1]);
            //  printf("msg_data%d: 0x%x\n",num,msg_data[num]);
            // printf("len4: %d\n",codelen + 8 -len_buff);
            crypto_hash(&ascon_hash_initstruct,msg_data,codelen + 8 -len_buff);
            hash_data = ascon_hash_initstruct.hash_data;
          }

      }
          

      //compare_result(case_num,hash_data);
         printf("last\n");
      printf("case  the hash is 0x%08x%08x%08x%08x%08x%08x%08x%08x\r\n", \
      hash_data[1],hash_data[0],hash_data[3],hash_data[2], \
      hash_data[5],hash_data[4],hash_data[7],hash_data[6]);

}

void aead_init_en(Ascon_Aead_En_InitTypeDef* ascon_aead_en_initstruct, u32 *msg_data,u32 blocknum)
{

    ascon_aead_en_initstruct->msg_block_num = blocknum;
    ascon_aead_en_initstruct->msg_addr = 0x00000000;
    ascon_aead_en_initstruct->aead_ctrl_mode = 0x00000001;
    ascon_aead_en_initstruct->aead_c_addr = 0x00000000;
    ascon_aead_en_initstruct->msg_data = msg_data;
    ascon_aead_en_initstruct->aead_a[0] = 0x04050607;
    ascon_aead_en_initstruct->aead_a[1] = 0x00010203;
    ascon_aead_en_initstruct->aead_k[0] = 0x0C0D0E0F;
    ascon_aead_en_initstruct->aead_k[1] = 0x08090A0B;  
    ascon_aead_en_initstruct->aead_k[2] = 0x04050607;  
    ascon_aead_en_initstruct->aead_k[3] = 0x00010203; 
    ascon_aead_en_initstruct->aead_n[0] = 0x0C0D0E0F;
    ascon_aead_en_initstruct->aead_n[1] = 0x08090A0B;  
    ascon_aead_en_initstruct->aead_n[2] = 0x04050607;  
    ascon_aead_en_initstruct->aead_n[3] = 0x00010203;                         
    ascon_aead_en_initstruct->aead_ctrl_en = ENABLE;
    Ascon_Aead_En_Init(ascon_aead_en_initstruct);
    Start_Ascon_Aead_En(ascon_aead_en_initstruct);
    Get_Ascon_Aead_En_Ct(ascon_aead_en_initstruct);

    // aead_data = ascon_aead_en_initstruct.aead_data;
}

void aead_init_de(Ascon_Aead_De_InitTypeDef *ascon_aead_de_initstruct, u32 *msg_data,u32 blocknum)
{

    ascon_aead_de_initstruct->msg_block_num = blocknum;
    ascon_aead_de_initstruct->msg_addr = 0x00000000;
    ascon_aead_de_initstruct->aead_ctrl_mode = 0x00000001;
    ascon_aead_de_initstruct->aead_c_addr = 0x00000000;
    ascon_aead_de_initstruct->msg_data = msg_data;
    ascon_aead_de_initstruct->aead_a[0] = 0x04050607;
    ascon_aead_de_initstruct->aead_a[1] = 0x00010203;
    ascon_aead_de_initstruct->aead_k[0] = 0x0C0D0E0F;
    ascon_aead_de_initstruct->aead_k[1] = 0x08090A0B;  
    ascon_aead_de_initstruct->aead_k[2] = 0x04050607;  
    ascon_aead_de_initstruct->aead_k[3] = 0x00010203; 
    ascon_aead_de_initstruct->aead_n[0] = 0x0C0D0E0F;
    ascon_aead_de_initstruct->aead_n[1] = 0x08090A0B;  
    ascon_aead_de_initstruct->aead_n[2] = 0x04050607;  
    ascon_aead_de_initstruct->aead_n[3] = 0x00010203;                         
    ascon_aead_de_initstruct->aead_ctrl_en = ENABLE;
    Ascon_Aead_De_Init(ascon_aead_de_initstruct);
    Start_Ascon_Aead_De(ascon_aead_de_initstruct);
    Get_Ascon_Aead_De_Pt(ascon_aead_de_initstruct);

    // aead_data = ascon_aead_en_initstruct.aead_data;
}

void aead_loop_de(Ascon_Aead_De_InitTypeDef *ascon_aead_de_initstruct, u32 *msg_data,u32 blocknum)
{
    ascon_aead_de_initstruct->msg_block_num = blocknum;
    ascon_aead_de_initstruct->msg_data = msg_data;                 
    ascon_aead_de_initstruct->aead_ctrl_en = ENABLE;
    Ascon_Aead_De_Init(ascon_aead_de_initstruct);
    Start_Ascon_Aead_De(ascon_aead_de_initstruct);
    Get_Ascon_Aead_De_Pt(ascon_aead_de_initstruct);
}

void aead_loop_en(Ascon_Aead_En_InitTypeDef* ascon_aead_en_initstruct, u32 *msg_data,u32 blocknum)
{

    ascon_aead_en_initstruct->msg_block_num = blocknum;
    ascon_aead_en_initstruct->msg_data = msg_data;                 
    ascon_aead_en_initstruct->aead_ctrl_en = ENABLE;
    Ascon_Aead_En_Init(ascon_aead_en_initstruct);
    Start_Ascon_Aead_En(ascon_aead_en_initstruct);
    Get_Ascon_Aead_En_Ct(ascon_aead_en_initstruct);
    // aead_data = ascon_aead_en_initstruct.aead_data;
}
//测试用例
u32 aead_get_code(u32 loop_size,u32 msg_data[],u32 *addr,u32 start_n)
{
    u32 i ;
    uint32_t buffer_data;
    //static uint32_t msg_data[CODENUM_U32+1];
    // printf("loop_size:0x%x\n",loop_size); 
    for ( i = 0; i < loop_size; i = i+2)
    {
            // printf("addr0:0x%x\n",*addr + ((i+1)<<2)); 
            buffer_data = REG32(*addr + ((i+1)<<2));
            // printf("buffer_data0:0x%x\n",buffer_data); 
            msg_data[i+start_n] = ((buffer_data & 0x000000ff) << 24) | ((buffer_data & 0x0000ff00) << 8) | ((buffer_data & 0x00ff0000) >> 8) | ((buffer_data & 0xff000000) >> 24);
            
            // printf("addr1:0x%x\n",*addr + (i<<2)); 
            buffer_data = REG32(*addr + (i<<2));
            // printf("buffer_data1:0x%x\n",buffer_data);  
            msg_data[i+1+start_n] = ((buffer_data & 0x000000ff) << 24) | ((buffer_data & 0x0000ff00) << 8) | ((buffer_data & 0x00ff0000) >> 8) | ((buffer_data & 0xff000000) >> 24);

    }
    //  msg_data[CODENUM_U32] = 0x00000000;
    //  msg_data[CODENUM_U32+1] = 0x80000000;
        //  for ( i = 0; i < CODENUM_U32; i=i+1)
        //  {
        //  printf("msg_data:0x%x\n",msg_data[i]);
        //   }
        // printf("before addr:%d\n",*addr);
        // printf("loop_size:%d\n",loop_size);
        // printf("loop_size<<2:%d\n",loop_size << 2);
     *addr = *addr + (loop_size << 2); //一定加括号，+优先级更高
    //  printf("addr:0x%x\n",*addr);
    //  return d_len-(loop_size << 2);
    return 0;
    
    
}

void run_ascon_aead_case(u8 case_num)
{
    static Ascon_Aead_En_InitTypeDef ascon_aead_en_initstruct;
    static Ascon_Aead_De_InitTypeDef ascon_aead_de_initstruct;
    
    //static u32 msg_data[512];
    u32 msg_data[256+1];

    u32 c_data[AEAD_CODENUM_U32];
     u32 p_data[AEAD_CODENUM_U32];
    static u32* aead_data;
    u32 i;
    u32 j=1;
    u32 num_u32_c;
    u32 addr_begin = ADDR_DATA_BEGIN;
    u32 block_num_remain = BLOCK_NUM;
    u32 *msg_p = msg_data;
    switch(case_num)
    {

        case 3://Count = 933
        {
            //printf("begin case !! \n");
            aead_get_code(256,msg_data,&addr_begin,0); 
            //  printf("get over !! \n");
            printf("block_num_remain %d\n",block_num_remain);
            aead_init_en(&ascon_aead_en_initstruct,msg_data,block_num_remain);
            //  printf("aead !! \n");
            aead_data = ascon_aead_en_initstruct.aead_data;
            if( block_num_remain > 128)
             { 
                num_u32_c = (128<<1);
                block_num_remain = block_num_remain - 128;
            }
            else
            {
              num_u32_c = (block_num_remain)<<1;
              block_num_remain = 0;
            }

            for (i = 0 ; i < num_u32_c;i++)
            {
                c_data[i] = *(aead_data+i);
               // printf("%d c data is %08x\r\n", i,c_data[i]);
            }
            // msg_p = msg_p + num_u32_c;
            //  printf("print \n");
            // printf("msg_p data is %08x\r\n",*msg_p);
            // printf("msg_data data is %08x\r\n",msg_data[i]);
            
            while (block_num_remain >= 128)
            {
               aead_get_code(256,msg_data,&addr_begin,0); 
               aead_loop_en(&ascon_aead_en_initstruct,msg_data,128);
               //这里 aead_data并没有重新赋地址,但结果也是正确的,因为这里依然指向前面结构体,地址未改变
                for (i = 0 ; i < 256;i++)
                {
                    c_data[i + (j<<8)] = *(aead_data+i);
                }
                // msg_p = msg_p + 256;
                block_num_remain = block_num_remain - 128;
                j = j + 1;
            }
            if(block_num_remain > 0)
            {
              aead_get_code(256,msg_data,&addr_begin,0); 
               aead_loop_en(&ascon_aead_en_initstruct,msg_data,block_num_remain);
                for (i = 0 ; i < block_num_remain*2;i++)
                {
                    c_data[i + (j<<8)] = *(aead_data+i);
                }
            }
             printf("ciphertext is : ");
            for (uint16_t i = 0; i < (AEAD_NUM_VALID>>2); i=i+2)
            {
                printf("%08x", c_data[i+1]);
                printf("%08x", c_data[i]);
            }
            printf("\n");
            printf("========================================\n");
            printf("\n");
            printf("========================================\n");
            printf("tee core process : ASCON-Hash Test\n");
            printf("========================================\n");
            run_ascon_hash((BLOCK_NUM<<3),c_data);
            printf("\n");
            printf("========================================\n");
            printf("tee core process : ASCON-Decryption Test\n");
            printf("========================================\n");
            u32 ree_addr = ADDR_DATA_BEGIN;
            u32 buffer_data = 0;
            for(i = 0 ; i < AEAD_CODENUM_U32;i = i + 2)
            {
            //    buffer_data = c_data[i+1];
               buffer_data = ((c_data[i+1] & 0x000000ff) << 24) | ((c_data[i+1] & 0x0000ff00) << 8) | ((c_data[i+1] & 0x00ff0000) >> 8) | ((c_data[i+1] & 0xff000000) >> 24);
            //   printf("buffer_data : 0x%x\n", buffer_data);
               REG32(ree_addr + (i<<2)) = buffer_data;
               
               buffer_data = ((c_data[i] & 0x000000ff) << 24) | ((c_data[i] & 0x0000ff00) << 8) | ((c_data[i] & 0x00ff0000) >> 8) | ((c_data[i] & 0xff000000) >> 24);
            //    buffer_data = c_data[i];
            //    printf("buffer_data : 0x%x\n", buffer_data);
               REG32(ree_addr + ((i+1)<<2)) = buffer_data;
            }
            // break;
        }
            case 4://Count = 933
        {
            addr_begin = ADDR_DATA_BEGIN;
            block_num_remain = BLOCK_NUM;
            //  printf("get code !! \n");
            aead_get_code(256,msg_data,&addr_begin,0); 
            //  printf("get over !! \n");
            printf("block_num_remain %d\n",block_num_remain);
            aead_init_de(&ascon_aead_de_initstruct,msg_data,block_num_remain);
            //  printf("aead !! \n");
            aead_data = ascon_aead_de_initstruct.aead_data;
            if( block_num_remain > 128)
             { 
                num_u32_c = (128<<1);
                block_num_remain = block_num_remain - 128;
            }
            else
            {
              num_u32_c = (block_num_remain)<<1;
              block_num_remain = 0;
            }

            for (i = 0 ; i < num_u32_c;i++)
            {
                p_data[i] = *(aead_data+i);
                //    printf("msg_data data is %08x\r\n",msg_data[i]);
                // printf("%d c0 data is %08x\r\n", i,c_data[i]);
            }
            // msg_p = msg_p + num_u32_c;
            //  printf("print \n");
            // printf("msg_p data is %08x\r\n",*msg_p);
            // printf("msg_data data is %08x\r\n",msg_data[i]);
            
            while (block_num_remain >= 128)
            {
               aead_get_code(256,msg_data,&addr_begin,0); 
               aead_loop_de(&ascon_aead_de_initstruct,msg_data,128);
                for (i = 0 ; i < 256;i++)
                {
                    p_data[i + (j<<8)] = *(aead_data+i);
                    //  printf("%d c1 data is %08x\r\n", i,c_data[i]);
                }
                // msg_p = msg_p + 256;
                block_num_remain = block_num_remain - 128;
                j = j + 1;
            }
            if(block_num_remain > 0)
            {
              aead_get_code(256,msg_data,&addr_begin,0); 
              aead_loop_de(&ascon_aead_de_initstruct,msg_data,block_num_remain);
                for (i = 0 ; i < block_num_remain*2;i++)
                {
                    p_data[i + (j<<8)] = *(aead_data+i);
                    // printf("%d c2 data is %08x\r\n", i,c_data[i]);
                }
            }
            printf("Decrypted data is : ");
            for (uint16_t i = 0; i < (AEAD_NUM_VALID>>2); i=i+2)
            {
                printf("%08x", p_data[i+1]);
                printf("%08x", p_data[i]);
            }
            printf("\n");

            u32 ree_addr = ADDR_DATA_BEGIN;
            u32 buffer_data = 0;
            for(i = 0 ; i < AEAD_CODENUM_U32;i = i + 2)
            {
            //    buffer_data = c_data[i+1];
               buffer_data = ((p_data[i+1] & 0x000000ff) << 24) | ((p_data[i+1] & 0x0000ff00) << 8) | ((p_data[i+1] & 0x00ff0000) >> 8) | ((p_data[i+1] & 0xff000000) >> 24);
            //   printf("buffer_data : 0x%x\n", buffer_data);
               REG32(ree_addr + (i<<2)) = buffer_data;
               
               buffer_data = ((p_data[i] & 0x000000ff) << 24) | ((p_data[i] & 0x0000ff00) << 8) | ((p_data[i] & 0x00ff0000) >> 8) | ((p_data[i] & 0xff000000) >> 24);
            //    buffer_data = c_data[i];
            //    printf("buffer_data : 0x%x\n", buffer_data);
               REG32(ree_addr + ((i+1)<<2)) = buffer_data;
            }
            printf("\n\n");
            //addr_begin = ADDR_DATA_BEGIN;
            //aead_get_code(4,msg_data,&addr_begin,0); 
            // sim_end();
            break;
        }

    }
}

int tee_main (void)
{
    int int_dma_flag=0;
    uint32_t index;
    uint32_t data_check=0;
    uint32_t msg_data[128]; 
    PMU_InitTypeDef pmu_initstruct;

    printf("tee core process : running safety procedures\n");
    //关闭iopmp
    *(volatile uint32_t *)(IOPMP_BASE+IOPMP_RULE_REG) = 0xFFFFFFFF;

    pmu_initstruct.pmu_set_iram_len = 0x00010000;
    pmu_initstruct.pmu_set_iram_addr = 0x90000000;
    pmu_initstruct.pmu_iram_dec_en = ENABLE;
    Init_Iram_Bus_Param(&pmu_initstruct);
    data_check = *(volatile uint32_t *) (0x0000004c);
    printf("tee core process : tee core data is %x\r\n",data_check);
    //在访问ree总线的iram时先复位ree pc指针
    *(volatile uint32_t *)(PMU_BASE+PMU_PC_REG) = 0x00000000;
    data_check = *(volatile uint32_t *) (0x9000004c);
    printf("tee core process : ree core data is %x\r\n",data_check);
    //打印提示信息 ： 唤醒REE core
    printf("========================================\n");
    printf("tee core process : ASCON-Encryption Test\n");
    printf("========================================\n");
    
    //唤醒REE core
    //测试hash算法
    // run_ascon_hash_case(0);
    // run_ascon_hash_case(1);
    // run_ascon_hash_case(2);
    // run_ascon_hash_case(3);
    // run_ascon_hash_case(4);
    // run_ascon_hash_case(5);
    // run_ascon_hash_case(6);
    // //测试aead算法
    run_ascon_aead_case(3);
    // run_ascon_aead_case(1);
    // run_ascon_aead_case(2);
    //run_ascon_aead_case(4);
    //少量数据的测试-------------------
    // msg_data[2] = 0x0C000000;
    // msg_data[3] = 0x08090A0B;
    // msg_data[0] = 0x04050607;
    // msg_data[1] = 0x00010203;
    // printf("msg data: 0x");
    // for (uint8_t i = 0; i < 3; i=i+2)
    // {
    //     printf("%x",msg_data[i+1]);
    //     printf("%x",msg_data[i]);
    // }
    // // printf("%x",msg_data[3]);
    // printf("\n");
    // run_ascon_hash(13,msg_data);
    //---------------------------------
    //run_ascon_aead_case(4);
    //run_ascon_aead_case(5);
    //run_ascon_aead_case(6);
    //run_ascon_aead_case(7);



    printf("tee core process : tee core wakes up ree core\n");
    Weak_Up_Ree_Core(0x90000000);
    while(1);


}

int ree_main (void)
{
//模拟开机自检
//clear timer interrut state
    // for (dma_jndex = 0;dma_jndex < DMA_BLK_NUM; dma_jndex++)
    // {
    //     for (dma_index = 0;dma_index < DMA_BY_DEP/4+1; dma_index++)
    //     {
    //         data_check = *(volatile uint32_t *) (DMA_BY_SRC_BASE + dma_jndex * (DMA_BY_DEP + 0x1) + dma_index * 4);
    //         printf("%x\r\n",data_check);
    //     }
    //     printf("ree core process : this is %d block!\n",dma_jndex);
    // }
    // for (dma_index = 0;dma_index < DMA_BY_DEP/4+1; dma_index++)
    // {
    //     data_check = *(volatile uint32_t *) (0x20025000+dma_index*4);
    //     printf("%x\r\n",data_check);
    // }
    printf("ree core process :test successfully\n");
    sim_end();
}